﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KC.Presentation.Mv8CMS
{
	public partial class HootsuiteCSV : Form
	{
		public HootsuiteCSV()
		{
			InitializeComponent();
		}
		private string cs;
		private DataTable dtExport = new DataTable();

		private void HootsuiteCSV_Load(object sender, EventArgs e)
		{
			cs = System.Configuration.ConfigurationManager.ConnectionStrings["MV8"].ConnectionString;
			ddlPosts.DataSource = KC.DataAccess.Global.Global.CallSql_Query(cs, "select Title, FriendlyName from Posts order by id desc");
			ddlPosts.ValueMember = "FriendlyName";
			ddlPosts.DisplayMember = "Title";

			dtExport.Columns.Add("DateTime", typeof(DateTime));
			dtExport.Columns.Add("Text");
			dtExport.Columns.Add("URL");

			dateTimePicker1.Value = DateTime.Parse(DateTime.Now.AddMinutes(15).ToString("MM/dd/yyyy HH:mm:00"));
			while (dateTimePicker1.Value.Minute % 5 != 0) dateTimePicker1.Value += TimeSpan.FromMinutes(1);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			DateTime dt = DateTime.Parse(dateTimePicker1.Value.ToString("MM/dd/yyyy HH:mm:00"));
			while (dt.Minute % 5 != 0)
				dt = dt.AddMinutes(-1);
			

			foreach (DataRow dr in dtExport.Rows)
				if ((DateTime)dr["DateTime"] > dt)
					dt = (DateTime)dr["DateTime"];

			DataRow dr2 = dtExport.NewRow();
			dr2["DateTime"] = dt.AddMinutes(trackBar1.Value * 5);
			dr2["Text"] = ((DataRowView)ddlPosts.SelectedItem)["Title"] + " " + txtHashtags.Text;
			dr2["URL"] = "http://www.kconnolly.net/Post.aspx?Title=" + ((DataRowView)ddlPosts.SelectedItem)["FriendlyName"].ToString();
			dtExport.Rows.Add(dr2);
			dgExport.DataSource = null;
			dgExport.DataSource = dtExport;
		}

		private void trackBar1_Scroll(object sender, EventArgs e)
		{
			label1.Text = (trackBar1.Value * 5).ToString() + " minutes apart";
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			for (int i = dgExport.Rows.Count - 1; i >= 0; i--)
				if (dgExport.Rows[i].Selected && ((DataRowView)dgExport.Rows[i].DataBoundItem)["ID"] == ((DataRowView)ddlPosts.SelectedItem)["ID"])
					dtExport.Rows.RemoveAt(i);
			dgExport.DataSource = null;
			dgExport.DataSource = dtExport;
 		}

		private void btnExport_Click(object sender, EventArgs e)
		{
			string csv = "";
			foreach (DataRow dr in dtExport.Rows) {
				csv += ((DateTime)dr["DateTime"]).ToString("dd/MM/yyyy HH:mm")+",";
				string text = dr["Text"].ToString().Replace(",","");
				while (text.Length + 25 > 140) text = text.Substring(0, text.Length - 1);
				csv += text + ",";
				csv += dr["URL"].ToString();
				csv += "\r\n"; }
			string filename = System.IO.Path.GetTempFileName();
			System.IO.File.Delete(filename);
			filename = filename.Replace(".tmp", ".csv");
			System.IO.File.WriteAllText(filename, csv);
			txtHashtags.Text = filename;

			if (MessageBox.Show("Clear existing export data?", "Question", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) {
				dgExport.DataSource = null;
				dtExport = new DataTable();
				dtExport.Columns.Add("DateTime", typeof(DateTime));
				dtExport.Columns.Add("Text");
				dtExport.Columns.Add("URL"); }
			else
				for (int i = 0; i < dtExport.Rows.Count; i++)
					dtExport.Rows[i]["DateTime"] = ((DateTime)dtExport.Rows[i]["DateTime"]).AddHours(4);


		}
	}
}
